package com.mac.sys.urtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class WeekUtils
{
    /**
     * get first date of given month and year
     *
     * @param year
     * @param month
     * @return
     */
    public static String getFirstDayOfMonth(int year, int month)
    {
        String monthStr = month < 10 ? "0" + month : String.valueOf(month);
        return year + "-" + monthStr + "-" + "01";
    }

    /**
     * get the last date of given month and year
     *
     * @param year
     * @param month
     * @return
     */
    public static String getLastDayOfMonth(int year, int month)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DATE, 1);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        return calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" +
                calendar.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * get Calendar of given year
     *
     * @param year
     * @return
     */
    private static Calendar getCalendarFormYear(int year)
    {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.YEAR, year);
        return cal;
    }

    /**
     * get start date of given week no of a year
     *
     * @param year
     * @param weekNo
     * @return
     */
    public static String getStartDayOfWeekNo(int year, int weekNo)
    {
        Calendar cal = getCalendarFormYear(year);
        cal.set(Calendar.WEEK_OF_YEAR, weekNo);

        String month = WeekUtils.getMonth2(cal.get(Calendar.MONTH) + 1);
        String day = WeekUtils.getDay2(cal.get(Calendar.DAY_OF_MONTH));
        return cal.get(Calendar.YEAR) + "-" + month + "-" +day;
        //return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" +
        //        cal.get(Calendar.DAY_OF_MONTH);

    }

    /**
     * get the end day of given week no of a year.
     *
     * @param year
     * @param weekNo
     * @return
     */
    public static String getEndDayOfWeekNo(int year, int weekNo)
    {
        Calendar cal = getCalendarFormYear(year);
        cal.set(Calendar.WEEK_OF_YEAR, weekNo);
        cal.add(Calendar.DAY_OF_WEEK, 6);

        String month = WeekUtils.getMonth2(cal.get(Calendar.MONTH) + 1);
        String day = WeekUtils.getDay2(cal.get(Calendar.DAY_OF_MONTH));
        return cal.get(Calendar.YEAR) + "-" + month + "-" + day;
    }

    public static String getDay2(int day)
    {
        String dayStr = String.valueOf(day);
        if(day<10)
        {
            dayStr =  "0" + day;
        }
        return dayStr;
    }

    public static String getMonth2(int month)
    {
        String monthStr = String.valueOf(month);
        if(month<10)
        {
            monthStr =  "0" + month;
        }
        return monthStr;
    }

    public static int getWeekNo(String date)
    {
        Calendar calendar = Calendar.getInstance();
        try
        {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date dat = format.parse(date);


            calendar.setFirstDayOfWeek(Calendar.MONDAY);
            calendar.setTime(dat);

        } catch (ParseException e)
        {
            e.printStackTrace();
        }
        return calendar.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     *通过日期获取week对象
     * param date 日期
     */
    public static Week getWeek(String date)
    {
        int year = Integer.valueOf(date.split("\\-")[0]);
        Week week = new Week();
        int weekNo = WeekUtils.getWeekNo(date);

        String startDay = WeekUtils.getStartDayOfWeekNo(year, weekNo);
        String endDay = WeekUtils.getEndDayOfWeekNo(year, weekNo);

        week.setYear(year);
        week.setStartDay(startDay);
        week.setEndDay(endDay);
        week.setWeekNo(weekNo);

        return week;
    }

    /**
     *通过年，周号，获取week对象
     * param year 年
     * param date 周号
     */
    public static Week getWeek(int year, int weekNo)
    {
        Week week = new Week();

        String startDay = WeekUtils.getStartDayOfWeekNo(year, weekNo);
        String endDay = WeekUtils.getEndDayOfWeekNo(year, weekNo);

        week.setYear(year);
        week.setStartDay(startDay);
        week.setEndDay(endDay);
        week.setWeekNo(weekNo);

        return week;
    }

    public static void main(String[] args)
    {
        String endDay = WeekUtils.getEndDayOfWeekNo(2020, 1);
        System.out.println(endDay);

        String startDay = WeekUtils.getStartDayOfWeekNo(2020, 1);
        System.out.println(startDay);
    }

}


